package gui.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import core.Configuration;

public class ColorPicker extends JPanel implements ActionListener {
	private JButton btnRed, btnBlue, btnGreen, btnYellow;
	private Color chosenColor;

	public ColorPicker(Color initColor) {
		this.setLayout(null);
		btnRed = new JButton();
		skinButton(btnRed, Color.RED);
		btnRed.setBounds(0, 0, 20, 15);
		btnRed.addActionListener(this);

		btnBlue = new JButton();
		skinButton(btnBlue, Color.BLUE);
		btnBlue.setBounds(25, 0, 20, 15);
		btnBlue.addActionListener(this);

		btnGreen = new JButton();
		skinButton(btnGreen, Color.GREEN);
		btnGreen.setBounds(50, 0, 20, 15);
		btnGreen.addActionListener(this);

		btnYellow = new JButton();
		skinButton(btnYellow, Color.YELLOW);
		btnYellow.setBounds(75, 0, 20, 15);
		btnYellow.addActionListener(this);
		setSelectedColor(initColor);
		this.add(btnRed);
		this.add(btnBlue);
		this.add(btnGreen);
		this.add(btnYellow);
		Dimension panelSize = new Dimension(100,15);
		this.setMinimumSize(panelSize);
		this.setPreferredSize(panelSize);
		this.setMaximumSize(panelSize);
		this.setVisible(true);
	}

	private void skinButton(JButton btn, Color clr) {
		btn.setText(null);
		btn.setBorder(BorderFactory.createLineBorder(
				Configuration.MENU_ITEM_BORDER, 1));
		btn.setBackground(clr);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		btnRed.setBorder(BorderFactory.createLineBorder(
				Configuration.MENU_ITEM_BORDER, 1));
		btnBlue.setBorder(BorderFactory.createLineBorder(
				Configuration.MENU_ITEM_BORDER, 1));
		btnGreen.setBorder(BorderFactory.createLineBorder(
				Configuration.MENU_ITEM_BORDER, 1));
		btnYellow.setBorder(BorderFactory.createLineBorder(
				Configuration.MENU_ITEM_BORDER, 1));
		if (e.getSource().equals(btnRed)) {
			setSelectedColor(Color.RED);
		} else if (e.getSource().equals(btnBlue)) {
			setSelectedColor(Color.BLUE);
		} else if (e.getSource().equals(btnGreen)) {
			setSelectedColor(Color.GREEN);
		} else if (e.getSource().equals(btnYellow)) {
			setSelectedColor(Color.YELLOW);
		}
	}
	
	public Color getSelectedColor(){
		return chosenColor;
	}
	
	public void setSelectedColor(Color c){
		if(c.equals(Color.RED)){
			chosenColor = c;
			btnRed.setBorder(BorderFactory.createLineBorder(
					Color.BLACK, 2));
		} else if(c.equals(Color.BLUE)){
			chosenColor = c;
			btnBlue.setBorder(BorderFactory.createLineBorder(
					Color.BLACK, 2));
		} else if(c.equals(Color.GREEN)){
			chosenColor = c;
			btnGreen.setBorder(BorderFactory.createLineBorder(
					Color.BLACK, 2));
		} else if(c.equals(Color.YELLOW)){
			chosenColor = c;
			btnYellow.setBorder(BorderFactory.createLineBorder(
					Color.BLACK, 2));
		}
	}
}
